package cz.muni.fi.pa165.business.service.utils;

import cz.muni.fi.pa165.api.dto.ForestVisitDTO;
import cz.muni.fi.pa165.api.dto.MushroomDTO;
import cz.muni.fi.pa165.business.entity.ForestVisit;
import cz.muni.fi.pa165.business.entity.Mushroom;
import java.util.ArrayList;
import java.util.List;

/**
 * Collection of transformation methods between ForestVisit and ForestVisitDTO
 * objects
 *
 * @author Matej Cimbora
 */
public class ForestVisitObjectTransformer {

    private ForestVisitObjectTransformer() {
    }

    public static ForestVisit forestVisitDTOtoForestVisit(ForestVisitDTO forestVisitDTO) {
        ForestVisit forestVisit = new ForestVisit();
        forestVisit.setId(forestVisitDTO.getId());
        forestVisit.setDescription(forestVisitDTO.getDescription());
        forestVisit.setDateOfVisit(forestVisitDTO.getDateOfVisit());
        forestVisit.setPicker(
                PickerObjectTransformer.pickerDTOToPicker(forestVisitDTO.getPickerDTO()));
        forestVisit.setForest(
                ForestObjectTransformer.forestDTOToForest(forestVisitDTO.getForestDTO()));

        if (forestVisitDTO.getMushroomsFound() != null) {
            forestVisit.setMushroomsFound(new ArrayList<Mushroom>());
            for (MushroomDTO mushroomDTO : forestVisitDTO.getMushroomsFound()) {
                forestVisit.getMushroomsFound().add(MushroomObjectTransformer.mushroomDTOToMushroom(mushroomDTO));
            }
        }
        return forestVisit;
    }

    public static ForestVisitDTO forestVisitToForestVisitDTO(ForestVisit forestVisit) {
        if (forestVisit == null) {
            return null;
        }
        ForestVisitDTO forestVisitDTO = new ForestVisitDTO();
        forestVisitDTO.setId(forestVisit.getId());
        forestVisitDTO.setDescription(forestVisit.getDescription());
        forestVisitDTO.setDateOfVisit(forestVisit.getDateOfVisit());
        forestVisitDTO.setPickerDTO(
                PickerObjectTransformer.pickerToPickerDTO(forestVisit.getPicker()));
        forestVisitDTO.setForestDTO(
                ForestObjectTransformer.forestToForestDTO(forestVisit.getForest()));

        if (forestVisit.getMushroomsFound() != null) {
            forestVisitDTO.setMushroomsFound(new ArrayList<MushroomDTO>());
            for (Mushroom mushroom : forestVisit.getMushroomsFound()) {
                forestVisitDTO.getMushroomsFound().add(MushroomObjectTransformer.mushroomToMushroomDTO(mushroom));
            }
        }
        return forestVisitDTO;
    }

    public static List<ForestVisitDTO> forestVisitListToForestVisitListDTO(List<ForestVisit> list) {
        List<ForestVisitDTO> dtoList = new ArrayList<>();
        for (ForestVisit fv : list) {
            dtoList.add(forestVisitToForestVisitDTO(fv));
        }
        return dtoList;
    }
}
